/*

OverloadPreventerTest
OverloadPreventer.voiceLaunched(1,10);
OverloadPreventer.getCurrentlyRunning;

*/

OverloadPreventer{

	classvar currentlyRunning = 0;

	*voiceLaunched{|cpuIndex, time|
		currentlyRunning = currentlyRunning + cpuIndex;
		SystemClock.sched(
			time, 
			{
				currentlyRunning = currentlyRunning - cpuIndex; 
				nil;
			}
		);
	}
	
	*getCurrentlyRunning{
		^currentlyRunning;
	}
	
	*isSafeToAdd{|proposedAddition|
		if(Server.default.avgCPU > 80){
			^false;
		};
		if(
			OverloadPreventer.getCurrentlyRunning + proposedAddition < SynthConstants.getMaxVoices,
			{^true},
			{^false}
		)
	}	

}